//
//  WmcShareWatermarkVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/8/10.
//

import UIKit
import YPBedrock

// MARK: - 扩展WmcShareWatermarkVC + WmcNavigatorProtocol

extension WmcShareWatermarkVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.shareWatermark]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcShareWatermarkVM()
        if let viewModel = parms.context as? WmcShareWatermarkVM {
            vm = viewModel
        }
        let vc = WmcShareWatermarkVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcShareWatermarkVC + WmcVMProtocol

extension WmcShareWatermarkVC: WmcVMProtocol {
    typealias VMType = WmcShareWatermarkVM
}

// MARK: - [WmcShareWatermarkVC]

class WmcShareWatermarkVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "水印分享"

        viewModel.loadData()
    }

    // Internal

    lazy var watermarkContentView: WmcWatermarkContentView = {
        let view = WmcWatermarkContentView()
        view.isUserInteractionEnabled = false
        view.backgroundColor = .wmc_grayH2_323233
        return view
    }()

    lazy var footerView: WmcShareWatermarkFooterView = {
        let view = WmcShareWatermarkFooterView()
        return view
    }()

    override func makeUI() {
        makeNavView()

        addSubview(watermarkContentView)
        watermarkContentView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.4125)
        }

        addSubview(footerView)
        footerView.snp.makeConstraints { make in
            make.top.equalTo(watermarkContentView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }

    override func bindViewAction() {
        // 移动水印到居中
        watermarkContentView.layoutWatermarksBlock = { [weak self] in
            guard let weakSelf = self else { return }
            let centerX = weakSelf.watermarkContentView.frame.width / 2
            let centerY = weakSelf.watermarkContentView.frame.height / 2
            weakSelf.watermarkContentView.moveWatermarkView(.init(x: centerX, y: centerY))
        }
        // 点击微信分享
        footerView.wechatItem.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            WmcPrint("水印分享点击了微信分享水印")
            weakSelf.checkLogin { [weak self] in // 校验登录
                guard let weakSelf = self else { return }
                weakSelf.viewModel.share(scene: .session,
                                         watermarkContentView: weakSelf.watermarkContentView)
            }
        }

        // 点击微信朋友圈分享
        footerView.friendsCircleItem.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            WmcPrint("水印分享点击了微信朋友圈分享水印")
            weakSelf.checkLogin { [weak self] in // 校验登录
                guard let weakSelf = self else { return }
                weakSelf.viewModel.share(scene: .friendCircle,
                                         watermarkContentView: weakSelf.watermarkContentView)
            }
        }
    }

    override func bindViewModel() {
        /// 水印发生变动
        viewModel.watermarkRelay.subscribe(onNext: { [weak self] value in
            guard let weakSelf = self,
                  let wmModel = value else { return }
            weakSelf.watermarkContentView.setWatermarkModel(wmModel)
        }).disposed(by: rx.disposeBag)
    }

    /// 校验登录
    func checkLogin(_ successBlock: @escaping YPBlock.Empty) {
        // TODO: 登录方法需要统一封装
        if !WmcLoginManager.shared.isLogin { // 没有登录
            if YPWmcManager.shared.isIndependentPage {
                WmcLoginManager.push(animated: true) {
                    successBlock()
                } failure: {
                    WmcHudManager.toast("登录失败")
                }
            } else {
                YPWmcManager.shared.delegate.loginDelegate?.loginNeed(.current, animated: true, success: {
                    successBlock()
                }, failure: {
                    WmcHudManager.toast("登录失败")
                })
            }
        } else { // 已登录
            successBlock()
        }
    }
}
